home *** CD-ROM | disk | FTP | other *** search
/ Aminet 52 / Aminet 52 (2002)(GTI - Schatztruhe)[!][Dec 2002].iso / Aminet / game / misc / xquiz-mos.lha / xquiz / xquiz-1.2.c < prev    next >
C/C++ Source or Header  |  2002-06-23  |  10KB  |  369 lines

  1. #include <stdio.h> /* fopen() */
  2. #include <string.h> /* strcmp(), strcpy() */
  3. #include <ctype.h> /* tolower() */ 
  4.  
  5.  
  6. /*
  7.        Copyright (C) 2002 Thomas Martinsen   
  8.  
  9.         This program is free software; you can redistribute it and/or modify
  10.         it under the terms of the GNU General Public License as published by
  11.         the Free Software Foundation; either version 2 of the License, or
  12.         (at your option) any later version.
  13.  
  14.         This program is distributed in the hope that it will be useful,
  15.         but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.         GNU General Public License for more details.
  18.  
  19.         You should have received a copy of the GNU General Public License
  20.         along with this program; if not, write to the Free Software
  21.         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  22. */
  23.  
  24.  
  25. /* #include <stdlib.h> */
  26. /* int system( const char *command ); */
  27.  
  28. char lang_q[100] = "english";
  29. char lang_q_current[100] = "english";
  30.     int counter = 0;
  31.     int correct = 0;
  32.     int failed = 0;
  33.     int state = 0;
  34.  
  35.  
  36. int loadcf() {
  37.  
  38. char cfline[100];
  39. char *c;
  40. char lang_q_test[100] = "lang_q";
  41. char yestest[100] = "yes";
  42. char notest[100] = "no";
  43. char lang_q_enabled[100];
  44.  
  45. FILE *cf;
  46. cf = fopen(".xquiz.cf","r");
  47. if (cf == NULL) { 
  48. cf = fopen("xquiz.cf.txt","w");
  49. fprintf(cf,"lang\n");
  50. fprintf(cf,"english");
  51. }
  52.     do {
  53.  
  54.  
  55.         c = fgets(cfline,100,cf);
  56.     if (c != NULL) {
  57.  if (c[strlen(c)-1] == '\n') c[strlen(c)-1] = 0; // remove trailing newline
  58.  
  59.         if (strcmp(lang_q_enabled,yestest) == 0) {
  60.     strcpy(lang_q,cfline); 
  61.     strcpy(lang_q_enabled,"no");
  62.        }
  63.  
  64.  
  65.     if (strcmp(cfline,lang_q_test) == 0) {
  66.     strcpy(lang_q_enabled,"yes");
  67.     continue;
  68.     }
  69.     
  70. }
  71. }
  72. while (c != NULL);
  73.  
  74.  
  75. }
  76.  
  77. int save() {
  78. FILE *fp;
  79. /* counter++; */
  80.  
  81. fp = fopen("xquiz.sv","w");
  82. if (fp == NULL) { printf("Could not write to save file, Changes isnt saved\n"); return 0; }
  83. fprintf(fp,"%d\n",correct);
  84. fprintf(fp,"%d\n",failed);
  85. fprintf(fp,"%d\n",counter);
  86. /* counter--; */
  87. fclose(fp);
  88. return 0;
  89. }
  90.  
  91.  
  92. int main(int argc,char **argv) {
  93.  
  94. char question[100];
  95. char answer[100];
  96. char correctanswer[100];
  97. char youranswer[100];
  98. char lastanswer[100] = "nope";
  99. char questiontest[100] = "question";
  100. char answertest[100] = "answer";
  101. char lastanswertest[100] = "lastanswer";
  102. char correctanswertest[100] = "correctanswer";
  103. char disableanswers[100];
  104. char disableanswersno[100] = "no";
  105. char lang_test[100] = "lang";
  106. char lang_q_enabled[100];
  107. char lang_q_yes[100];
  108.  
  109. char *ptr11;
  110. char *ptr12;
  111. char *ptr21;
  112. char *ptr22;
  113. char *ptr31;
  114. char *ptr32;
  115. char *curcwd;
  116. char oneword[1000];
  117. char *c;
  118.  
  119. FILE *fp1;
  120. FILE *readf;
  121. FILE *svreset;
  122.  
  123.  
  124.  
  125.  
  126.  
  127. loadcf();
  128.  
  129. printf("XQuiz 1.2, Copyright (C) 2002 Thomas Martinsen\n");
  130. printf("XQuiz comes with ABSOLUTELY NO WARRANTY,\n");
  131. printf("This is free software, and you are welcome\n");
  132. printf("to redistribute it under certain conditions.\n");
  133. printf("Read LICENSE for details.\n"); 
  134. printf("http://xquiz.cjb.net\n");
  135. printf("tech@navn.no\n\n");
  136.  
  137.  
  138. readf = fopen("xquiz.sv","r");
  139. if (readf == NULL) {  }
  140. else fscanf(readf, "%d\n%d\n%d", &correct, &failed, &state);
  141.  
  142.  
  143.     /* list the lines in the question file */
  144.  
  145.     
  146.    if(argc>1) { fp1 = fopen(argv[1],"r"); } 
  147.     else fp1 = fopen("xquiz.db.txt","r");
  148.     if (fp1 == NULL) { printf("Couldnt read the database file (xquiz.db), remember this shall be in your current directory.\n"); return 0; }
  149.     do {
  150.  
  151.  
  152.         c = fgets(oneword,100,fp1);
  153.         if (c != NULL) {
  154.     if (counter++ < state) { continue; }
  155.  
  156.             if (c[strlen(c)-1] == '\n') c[strlen(c)-1] = 0; // remove trailing newline
  157.      
  158.  
  159.             /* check if the line contains "lang", wich mean the next line is a answer */
  160.             if (strcmp(oneword,lang_test) == 0) {
  161.         strcpy(lang_q_enabled,lang_q_yes);
  162.         continue;
  163.         }
  164.  
  165.  
  166.     if(strcmp(lang_q_enabled,lang_q_yes) == 0) {
  167.     strcpy(lang_q_current,oneword);
  168.      strcpy(lang_q_enabled,"no");
  169.     continue;
  170.     }
  171.  
  172.     if (strcmp(lang_q_current,lang_q) != 0) { continue; }
  173.  
  174.         /* check if the line contains lastanswercheck, wich its the last answer */
  175.             cont: if (strcmp(lastanswer,lastanswertest) == 0) {
  176.                 /*  if (lastanswer == lastanswercheck) { */
  177.  
  178.  
  179.  
  180.               /* remove variables */
  181.         strcpy(question,"nope");
  182.                 strcpy(answer,"nope");
  183.                 strcpy(lastanswer,"nope");
  184.  
  185.  
  186.                 if (strcmp(correctanswer,correctanswertest) == 0) {
  187.  
  188.                     continue;
  189.                 }
  190.  
  191.                 /* if the users answer match with this line, do this */
  192.  
  193.  
  194.                 for(ptr11 = youranswer; *ptr11; ptr11++) *ptr11 = tolower(*ptr11);
  195.                 for(ptr12 = oneword; *ptr12; ptr12++) *ptr12 = tolower(*ptr12);
  196.  
  197.                 if (strcmp(oneword,youranswer) == 0) {
  198.  
  199.                     /* correctanswer[100] = correctanswercheck[100]; */
  200.                     printf("Correct!\n");
  201.                     correct ++;
  202.                     printf("Correct Answers: %d | Failed: %d\n\n", correct, failed);
  203.                     /*
  204.                     ofstream savewrite("quizzie.save");
  205.                     savewrite << correct << "\n";
  206.                     savewrite << failed << "\n";
  207.                     savewrite.close();
  208.                     */
  209.             save();
  210.                 }
  211.  
  212.  
  213.                 /* if not the user has failed, since this is the _last answer_ */
  214.  
  215.                 for(ptr21 = youranswer; *ptr21; ptr21++) *ptr21 = tolower(*ptr21);
  216.                 for(ptr22 = oneword; *ptr22; ptr22++) *ptr22 = tolower(*ptr22);
  217.  
  218.                 if (strcmp(oneword,youranswer) != 0) {
  219.  
  220.                     printf("You failed!\n");
  221.                     failed ++;
  222.                     printf("Correct Answers: %d | Failed: %d\n\n", correct, failed);
  223.                     /*
  224.                     ofstream savewrite("quizzie.save");
  225.                     savewrite << correct << "\n";
  226.                     savewrite << failed << "\n";
  227.                     savewrite.close();
  228.                     */
  229.             save();
  230.                 }
  231.  
  232.                /* remove all variables */
  233.                 strcpy(question,"nope");
  234.                 strcpy(answer,"nope");
  235.                 strcpy(lastanswer,"nope");
  236.  
  237.  
  238.                 /* goto next line */
  239.                 continue;
  240.  
  241.             }
  242.  
  243.  
  244.             /* check if its a question */
  245.             if (strcmp(question,questiontest) == 0) {
  246.  
  247.  
  248.  
  249.                 /* if (question == "question") { */
  250.  
  251.                 /* remove variables */
  252.  
  253.                 strcpy(question,"nope");
  254.                 strcpy(answer,"nope");
  255.                 strcpy(lastanswer,"nope");
  256.         strcpy(correctanswer,"nope");
  257.  
  258.  
  259.                 /*question[100] = disable[100];
  260.                 answer[100] = disable[100];
  261.                 lastanswer[100] = disable[100];
  262.                 correctanswer[100] = disable[100];*/
  263.  
  264.                 /* print out question */
  265.                 printf("%s\n", oneword);
  266.  
  267.                 /* asks for answer */
  268.                 printf("Enter answer: ");
  269.         fgets(youranswer,100,stdin);
  270.         if (youranswer[strlen(youranswer)-1] == '\n') youranswer[strlen(youranswer)-1] = 0;
  271.                 /* goto next line */
  272.                 continue;
  273.             }
  274.  
  275.             /* check if its a answer */
  276.             if (strcmp(answer,answertest) == 0) {
  277.  
  278.  
  279.  
  280.                 /*  if (answer == answercheck) { */
  281.  
  282.                 /* remove all variables */
  283.                 strcpy(question,"nope");
  284.                 strcpy(answer,"nope");
  285.                 strcpy(lastanswer,"nope");
  286.  
  287.                 /* if the user has already answered corect, goto next line */
  288.                 if (strcmp(correctanswer,correctanswertest) == 0) {
  289.  
  290.                     /*    if (correctanswer == correctanswercheck) { */
  291.                     continue;
  292.                 }
  293.  
  294.                 /* if the users answer match with this line, do this */
  295.  
  296.                 for(ptr31 = youranswer; *ptr31; ptr31++) *ptr31 = tolower(*ptr31);
  297.                 for(ptr32 = oneword; *ptr32; ptr32++) *ptr32 = tolower(*ptr32);
  298.  
  299.                 if (strcmp(oneword,youranswer) == 0) {
  300.         strcpy(correctanswer,"correctanswer");
  301.                     /* correctanswer[100] = correctanswercheck[100]; */
  302.                     printf("Correct!\n");
  303.                     correct ++;
  304.                     printf("Correct Answers: %d | Failed: %d\n\n", correct, failed);
  305.                     /*
  306.                     ofstream savewrite("quizzie.save");
  307.                     savewrite << correct << "\n";
  308.                     savewrite << failed << "\n";
  309.                     savewrite.close();
  310.                     */
  311.             save();
  312.                 }
  313.  
  314.                 /* remove some variables */
  315.                 strcpy(question,"nope");
  316.                 strcpy(answer,"nope");
  317.                 strcpy(lastanswer,"nope");
  318.  
  319.  
  320.                 /* then goto next line */
  321.                 continue;
  322.             }
  323.  
  324.             /* check if the line contains "question", wich mean the next line is a question */
  325.         if(strcmp(oneword, questiontest) == 0) {
  326.         strcpy(question,"question");
  327.                 /* question[100] = questioncheck[100]; */
  328.  
  329.                 /* goto next line */
  330.                 continue;
  331.             }
  332.  
  333.  
  334.  
  335.             /* check if the line contains "answer", wich mean the next line is a answer */
  336.             if (strcmp(oneword,answertest) == 0) {
  337.  
  338.                 /*    if (oneword == answercheck) { */
  339.         strcpy(answer,"answer");
  340.                 /* answer[100] = answercheck[100]; */
  341.  
  342.                 /* goto next line */
  343.                 continue;
  344.             }
  345.  
  346.             /* check if the line contains lastanswercheck[100], wich mean the next line is the _last answer_ */
  347.             if (strcmp(oneword,lastanswertest) == 0) {
  348.  
  349.                 /* if (oneword == lastanswercheck) { */
  350.         strcpy(lastanswer,"lastanswer");
  351.             }
  352.  
  353.         }
  354.  
  355.     }
  356.  
  357.     while (c != NULL);
  358.  
  359. svreset = fopen("xquiz.sv","w");
  360. if (svreset == NULL) { printf("Could not save save file, Changes isnt saved"); return 0; }
  361. fprintf(svreset,"%d\n%d\n0\n", counter, failed);
  362. fclose(svreset);
  363.  
  364.     fclose(fp1);
  365.  
  366.  
  367. return 0;
  368. }
  369.